package rs.in.idemdo;

import java.util.ArrayList;
import java.util.HashMap;

import rs.in.idemdo.db.IdemDoDbAdapter;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class PodesavanjaActivity extends Activity {

	private static final String NAZIV_POPUSTA = "np";
	private IdemDoDbAdapter mDbAdapter;
	private Cursor mPovlastice;

	private class CheckBoxBinder implements ViewBinder {

		public boolean setViewValue(View view, Object data,
				String textRepresentation) {
			final CheckBox cb = (CheckBox) view;
			cb.setText((String) data);
			return true;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.podesavanja);
		setTitle(R.string.naslov_podesavanja);

		mDbAdapter = new IdemDoDbAdapter(this);
		mDbAdapter.open();

		final ArrayList<HashMap<String, String>> naziviPovlastica = new ArrayList<HashMap<String, String>>();
		mPovlastice = mDbAdapter.fetchInfo("popust");
		final Cursor c = mPovlastice;
		final int column_index = c
				.getColumnIndex(IdemDoDbAdapter.INFO_VREDNOST_PARAMETRA_STRING);

		for (; !c.isAfterLast(); c.moveToNext()) {
			String column_string = c.getString(column_index);
			HashMap<String, String> mapa = new HashMap<String, String>();
			mapa.put(NAZIV_POPUSTA, column_string);
			naziviPovlastica.add(mapa);
		}

		final String[] from = new String[] { NAZIV_POPUSTA };
		final int[] to = new int[] { R.id.cb_popust };

		final ListView lv = (ListView) findViewById(R.id.lv_kategorije_popusta);

		final SimpleAdapter sa = new SimpleAdapter(this, naziviPovlastica,
				R.layout.rezultati_pretrage_stavka, from, to);
		sa.setViewBinder(new CheckBoxBinder());
		lv.setAdapter(sa);
	}

	@Override
	protected void onPause() {
		super.onPause();
		final CheckBox bus = (CheckBox) findViewById(R.id.cb_bus);
		final CheckBox voz = (CheckBox) findViewById(R.id.cb_voz);

		final RadioGroup linije = (RadioGroup) findViewById(R.id.rg_linije);
		final RadioButton rb = (RadioButton) findViewById(linije
				.getCheckedRadioButtonId());

		final ListView lv = (ListView) findViewById(R.id.lv_kategorije_popusta);
		final int velicina = lv.getCheckedItemPositions().size();

		String povlastice = "";
		for (int i = 0; i < velicina; i++) {
			final CheckBox cb = (CheckBox) lv.getChildAt(i);
			if (cb.isChecked()) {
				povlastice += cb.getText().toString() + "|";
			}
		}

		final String strBus = bus.isChecked() ? "bus" : "";
		final String strVoz = voz.isChecked() ? "voz" : "";
		final String vrednost = strBus + "|" + strVoz;
		mDbAdapter.updateInfoByNaziv("tip", vrednost, 0);
		mDbAdapter.updateInfoByNaziv("direktne", rb.getTag().toString(), 0);
		mDbAdapter.updateInfoByNaziv("povlastice", povlastice, 0);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

}
